const Koa = require('koa')
const loggerAsync = require('./middlewares/loggerAsync')
const router = require('./routes')
const bodyParser = require('koa-bodyparser')
var cors = require('koa2-cors')
const path = require('path')
const static = require('koa-static')

const app = new Koa()
// 打印日记
app.use(loggerAsync)
// 处理跨域问题
app.use(
  cors({
    origin: function(ctx) {
      return '*'
    }
    // exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    // maxAge: 5,
    // credentials: true,
    // allowMethods: ['GET', 'POST', 'DELETE'],
    // allowHeaders: ['Content-Type', 'Authorization', 'Accept']
  })
)

// 处理静态资源
// 静态资源目录对于相对入口文件app.js的路径
const staticPath = './static'
app.use(static(path.join(__dirname, staticPath)))
// 解析post请求体
app.use(bodyParser())
//解析路由
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000, () => {
  console.log('服务启动成功:localhost:3000')
})
